
--FORWARD COMPATIBILITY
table.unpack = unpack
--
cs2d = {}
cs2d.hook_dump = {}
function cs2d.addhook(h,f)
	local d = cs2d.hook_dump
	if not d[h] then
		d[h] = setmetatable({}, {__call = function(t,...)
				local r
				for k,v in pairs(t) do r = v(...) end
				return r
		end})
		addhook(h,"cs2d.hook_dump."..h)
	end
	for i=1,#d[h]+1 do
		if not d[h][i] then d[h][i] = f end
	end
end
function cs2d.freehook(h,f)
	local d = cs2d.hook_dump
	if d[h] then
		for k,v in pairs(d[h]) do
			if v == f then d[h][k] = nil end
		end
	end
end

--[1] = function, [2] = parameters, [3] = count
cs2d.timer_dump = {}

function cs2d.timer_call(i)
	local tmr = cs2d.timer_dump[tonumber(i)]
	tmr[1](table.unpack(tmr[2]))
	if tmr[3] ~= 1 then
		tmr[3] = tmr[3] - 1
	else
		cs2d.timer_dump[tonumber(i)] = nil
	end
end

function cs2d.timer(t, f, c, ...)
	local i = #cs2d.timer_dump +1
	for j = 1, #cs2d.timer_dump do
		if not cs2d.timer_dump[j] then
			i = j
			break
		end
	end
	cs2d.timer_dump[i] = {f,arg,c}
	timer(t,"cs2d.timer_call",tostring(i), c)
	return i
end

function cs2d.freetimer(f)
	if type(f) == "function" then
		for k,v in pairs(cs2d.timer_dump) do
			if v[1] == f or not f then
				cs2d.timer_dump[k] = nil
				freetimer("cs2d.timer_call",tostring(k))
			end
		end
	else
		cs2d.timer_dump[tonumber(i)] = nil
		freetimer("cs2d.timer_call",tostring(i))
	end
end